<?php
!defined("PHOTO_ADMIN") && exit("ACCESS DENIED");
$job = empty($_GET['job']) ? "list" : $_GET['job'];
include S_ROOT."include/image.class.php";
$url = "index.php?do=image";
if($job=="list"){
	$page = empty($_GET['page']) ? 1 : $_GET['page'];
	$pageSize = empty($_GET['pageSize']) ? 5 : $_GET['pageSize'];
	$option =  $_POST['option'];
	!$option && $option =  $_GET['option'];
	$keyword = $_POST['keyword'];
	!$keyword && $keyword =  $_GET['keyword'];


	switch($option){
		case "1":
			$author  = $keyword;
			break;
		case "2":
			$content = $keyword;
			break;
		case "3":
			$title = $keyword;
			break;
	}
	
	$where = " WHERE ifdelete=0";
	if(!empty($author)){
		$where .= " AND author='$author'";
	}
	if(!empty($content)){
		$where .= " AND  description LIKE '%$content%'";
	}

	if(!empty($title)){
		$where .= " AND  title LIKE '%$title%'";
	}
	
	$limit = " LIMIT ".($page-1)*$pageSize.",".$pageSize;
	if(empty($_GET['order']) && empty($_POST['order'])){
		$order = "createtime";
	}else if(!empty($_GET['order'])){
		$order = $_GET['order'];
	}else if(!empty($_POST['order'])){
		$order = $_POST['order'];
	}
	$sql = "SELECT title,author,phone,content,description,id,imageurl,votenum from ".tname('image')." $where ORDER BY $order DESC $limit";
	$query = $db->query($sql);
	$result = $db->fetch_all($query);
	
	$countSql = "SELECT COUNT(*) AS amount from ".tname('image')." $where ORDER BY createtime DESC "; 
	$countRs = $db->get_one($countSql);
	$pagetag = multi($countRs['amount'],$pageSize,$page,"index.php?do=image&job=list&option=$option&keyword=".urlencode($keyword));
	

	include atemplate("do_image_list");
}else if($job=="edit"){
	if(empty($_POST['step'])){
		$id = $_GET['id'];
		if(empty($id)) adminmessage('非法ID');
		$info = $db->get_one("SELECT title,author,imageurl,content,description,phone,votenum,id FROM  ".tname('image')."  WHERE id='".$id."'");
		extract($info);
		include atemplate("do_image_edit");
	}else{
		include(S_ROOT."data/dataconfig.php");
		$id = $_POST['id'];
		$data['title'] = trim($_POST['title']);
		$data['author'] = trim($_POST['author']);
		$data['content'] = $_POST['content'];
		$data['votenum'] = intval($_POST['votenum']);
		$data['phone'] = $_POST['phone'];
		$data['description'] = $_POST['description'];

		$fileurl = $_FILES['imageurl'];
		if ($id && !$fileurl['name']) {
			$fileurl = array();
		}
		if (!id && empty($fileurl['name'])) {
			adminmessage('请上传图片');
		}
		
				
		/**
		 * 限制文件大小 1M以内
		 **/
		if($fileurl['size']/1024/1024>1){
			showmessage('图片大小不能超过1M！');
		}


		if (!empty($fileurl)) {
			if ($fileurl['error'] !== 0) {
				adminmessage('上传图片发生错误');
			}else {
				$extname = fileext($fileurl['name']);
				$allow = array('jpg','jpeg','gif','png','tif','bmp');
				if(!in_array($extname,$allow)) {
					adminmessage('请上传以'.join(',',$allow)."结尾的图片");
				}
				$fileName  = 'upload/img/'.md5(time());
				$uploadurl = $fileName.'.'.$extname;
				$thumbUrl  = $fileName.'_thumb.'.$extname;
				move_uploaded_file($fileurl['tmp_name'],S_ROOT.$uploadurl);
				$tp = new image();
				$tp->thumb(S_ROOT.$uploadurl,S_ROOT.$thumbUrl,$_CONFIG['thumbwidth'],$_CONFIG['thumbheight']);
				$data['imageurl'] =  $uploadurl;
				$data['thumburl'] =  $thumbUrl;
			}
		}
	
		if (!$id) {
			inserttable('image',$data);
			adminmessage('添加图片成功',$url);
		}else{
			updatetable('image',$data,array('id'=>$id));
			adminmessage('修改图片成功',$url);
		}

		adminmessage("修改成功");
	}
}else if($job=="delete"){
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	}else{
		$db->query('update '.tname('image').' set ifdelete=1 where id='.$id);
		if ($db->affected_rows()) {
			adminmessage('删除图片成功',$url);
		}else {
			adminmessage('删除图片失败',$url);
		}
	}
}
?>